#!/bin/env python3
# -*- coding:utf-8 -*-
"""
    [模块名]
    Add By :e4ting e4ting@gmail.com 2023-09-24 16:33:29
"""
import sys,os
import json,time
from pdb import set_trace as strace
from traceback  import format_exc as dumpstack
from textwrap import dedent

from flask_restful import Resource
from flask         import redirect,request,make_response

from core.webhook.events import EventTemp
from e4ting          import util,log

class WebEventTypes(Resource):
    ''' 机器人种类 '''
    #----------------------------------------------------------------------
    def __init__(self):
        super(WebEventTypes, self).__init__()

    def get(self, **kwargs):
        data = EventTemp().list_types()
        data = [ dict(value=_["name"], label=_["desc"], text=_["desc"]) for _ in data]
        return dict(code=200, data=data, total=len(data))

    def put(self, id=None, **kwargs):
        log.info(id)
        data = request.form or request.json
        log.info(data)

        from core.webhook.events import EventTemp
        from core.webhook.hooks  import HookSender

        event = EventTemp().get_type(id)
        HookSender.execute_callbacks(data, event, title=event.desc)
        return dict(code=200, data="ok")

    def post(self, id=None, **kwargs):
        # log.info(id)
        # data = request.form or request.json
        # log.info(data)
        return self.put(id, **kwargs)

